package com.yanqu.road.server.cmd.yrank.union;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.yrank.UnionYRank;
import com.yanqu.road.entity.rank.yrank.UserYRank;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.activity.StrongholdActivityProto;
import com.yanqu.road.pb.orewaractivity.OreWarActivityProto;
import com.yanqu.road.pb.rank.CrossUnionActivityRankProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.pb.tradewaractivity.TradeWarActivityProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityInfoMgr;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuActivity;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_CONTRIBUTION, desc = "")
public class CrossYRankGetUnionRankContributionCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        YRankProto.YRankReqMsg reqMsg = YRankProto.YRankReqMsg.parseFrom(packet.getBytes());
        Integer conditionType = Integer.valueOf(reqMsg.getIdKey());

        if (conditionType == eGamePlayerEventType.BiaoJuUnionExpRank.getValue()) {
            CrossBiaoJuActivity activity = CrossBiaoJuMgr.getActivity(reqMsg.getActivityId(), reqMsg.getGroupId());
            if (activity != null) {
                activity.sendUnionRankMemberContribution(packet.getPlayerId(), serverId, reqMsg.getSeeUnionUid());
            }
            return;
        }

        UnionYRank unionYRank = CrossYRankMgr.getUnionYRank(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey(), reqMsg.getSeeUnionUid());
        List<UserYRank> list = new ArrayList<>();
        if (unionYRank != null) {
            list = CrossYRankMgr.getSortUnionContributionList(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey(), reqMsg.getSeeUnionUid());
        }

        if (conditionType == eGamePlayerEventType.UnionExpRiseRank.getValue()) {
            ActivityProto.UnionRankMemberConditionDataListMsg.Builder contributionBuilder = ActivityProto.UnionRankMemberConditionDataListMsg.newBuilder();
            for (UserYRank rank : list) {
                contributionBuilder.addDataList(ActivityProto.UnionRankMemberConditionDataTempMsg.newBuilder()
                        .setUserId(rank.getUserId())
                        .setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(rank.getUserBaseInfo()))
                        .setValue(rank.getValue().toString())
                        .setModifyTime(rank.getUpdateTime())
                );
            }
            CrossUnionActivityRankProto.CrossUnionActivityConditionUnionMemberDataRespMsg.Builder builder = CrossUnionActivityRankProto.CrossUnionActivityConditionUnionMemberDataRespMsg.newBuilder();
            builder.setRet(0);
            builder.setDataList(contributionBuilder);
            builder.setParam(unionYRank != null && !StringUtils.isNullOrEmpty(unionYRank.getParam()) ? Integer.valueOf(unionYRank.getParam()) : 0);
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_CROSS_ACTIVITY_UNION_MEMBER_CONDITION_DATA, builder);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
            return;
        }



        ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder contributionBuilder = ActivityProto.ActivityConditionUnionMemberDataListMsg.newBuilder();
        for (UserYRank rank : list) {
            contributionBuilder.addDataList(ActivityProto.ActivityConditionUnionMemberDataTempMsg.newBuilder()
                    .setUserId(rank.getUserId())
                    .setNickName(rank.getUserBaseInfo().getNickName())
                    .setValue(rank.getValue().toString())
                    .setModifyTime(rank.getUpdateTime())
            );
        }

        ActivityInfo activityInfo = ActivityInfoMgr.getActivityInfo(activityId);
        int actvityType = 0;
        if(null != activityInfo){
            actvityType = activityInfo.getType();
        }
        if (actvityType == eActivityType.OreWarActivity.getValue()){
            OreWarActivityProto.OreWarActivityConditionUnionMemberDataRespMsg.Builder msg = OreWarActivityProto.OreWarActivityConditionUnionMemberDataRespMsg.newBuilder();
            msg.setRet(0);
            msg.setDataList(contributionBuilder);
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_ORE_WAR_ACTIVITY_MEMBER_CONDITION_DATA, msg);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
            return;
        }else if (actvityType == eActivityType.CrossTradeWarActivity.getValue()){
            TradeWarActivityProto.TradeWarActivityConditionUnionMemberDataRespMsg.Builder tradeWarBuilder = TradeWarActivityProto.TradeWarActivityConditionUnionMemberDataRespMsg.newBuilder();
            tradeWarBuilder.setRet(0);
            ActivityProto.UnionRankMemberConditionDataListMsg.Builder rankListBuilder =  ActivityProto.UnionRankMemberConditionDataListMsg.newBuilder();

            for (UserYRank rank : list) {
                ActivityProto.UnionRankMemberConditionDataTempMsg.Builder rankBuilder = ActivityProto.UnionRankMemberConditionDataTempMsg.newBuilder();
                rankBuilder.setUserId(rank.getUserId());
                rankBuilder.setValue(rank.getValue().toString());
                rankBuilder.setModifyTime(rank.getUpdateTime());
                UserBaseInfo userBaseInfo = rank.getUserBaseInfo();
                rankBuilder.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                rankListBuilder.addDataList(rankBuilder);
            }
            tradeWarBuilder.setDataList(rankListBuilder);
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_TRADE_WAR_ACTIVITY_MEMBER_CONDITION_DATA, tradeWarBuilder);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
            return;
        }

        StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.Builder builder = StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.newBuilder();
        builder.setRet(0);
        builder.setDataList(contributionBuilder);
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_GENERAL_ACTIVITY_MEMBER_CONDITION_DATA, builder);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
    }

    @Override
    public void sendData() {

    }
}
